define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'party/partygoods/index' + location.search,
                    add_url: 'party/partygoods/add',
                    edit_url: 'party/partygoods/edit',
                    del_url: 'party/partygoods/del',
                    multi_url: 'party/partygoods/multi',
                    import_url: 'party/partygoods/import',
                    table: 'qsy_party',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'big_type', title: __('Big_type'),searchList:pidList,formatter: Table.api.formatter.status,},
                        {field: 'type.name', title: __('Small_type'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'cover_img', title: __('Cover_img'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'label', title: __('Label'), operate: false},
                        // {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'sold_num', title: __('Sold_num'), operate:'BETWEEN'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'code', title: __('Code'), operate:false,  events: Table.api.events.image,  formatter: Table.api.formatter.image},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name: 'download',
                                    classname: 'btn btn-xs btn-info btn-download',
                                    text: "下载二维码",
                                    icon: "",
                                }
                                ],
                            events: {
                                'click .btn-delone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    Layer.confirm(
                                        __('删除后二维码将会失效，确定删除此项?'),
                                        {
                                            icon: 3,
                                            title: __('Warning'),
                                            offset: [top, left],
                                            shadeClose: true,
                                            btn: [__('OK'), __('Cancel')]
                                        },
                                        function (index) {
                                            var table = $(that).closest('table');
                                            var options = table.bootstrapTable('getOptions');
                                            Table.api.multi("del", row[options.pk], table, that);
                                            Layer.close(index);
                                        }
                                    );
                                }, 'click .btn-editone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.edit_url;
                                    Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
                                },
                                'click .btn-download': function (e, value, row, index) {
                                    const aLink = document.createElement('a')
                                    aLink.download = name
                                    aLink.href = row.code
                                    aLink.dispatchEvent(new MouseEvent('click', {}))
                                },
                            },
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
